﻿Imports System.Collections.ObjectModel
Imports System.Collections.Specialized
Imports System.Windows.Input

Partial Public Class VMTableauSuivi
    Inherits VMBase
    Implements OMANE.Common.UI.IViewModelTabSection

    Dim _InitialisationTables As New ObservableCollection(Of InitialisationTable)
    Dim _OutilProvs As New OutilProvCollection
    Dim _GenerateOutilProvCommand As ICommand = New SCommand(AddressOf GenerateOutilProv)



    Public ReadOnly Property InitialisationTables As ObservableCollection(Of InitialisationTable)
        Get
            Return Me._InitialisationTables
        End Get
    End Property

    Public ReadOnly Property OutilProvs As OutilProvCollection
        Get
            Return Me._OutilProvs
        End Get
    End Property

    Public ReadOnly Property GenerateOutilProvCommand As ICommand
        Get
            Return _GenerateOutilProvCommand
        End Get
    End Property

    Sub GenerateOutilProv(ByVal p As Object)
        OutilProvs.Add(New OutilProv("Excel_" & Now.ToShortTimeString()))
    End Sub


    Sub Refresh()
        For Each t In InitialisationTables
            t.RefreshState()
        Next
    End Sub

End Class

Public Class InitialisationTable
    Inherits VMBase

    Dim _Name As String
    Dim _filepath As String
    Dim _State As String
    Dim _OpenLocationCommand As ICommand = New SCommand(AddressOf OpenLocation)

    Sub New(ByVal name As String, ByVal filepath As String)
        Me._Name = name
        Me._filepath = filepath
        Me._State = "default"
    End Sub

    Public ReadOnly Property OpenLocationCommand As ICommand
        Get
            Return _OpenLocationCommand
        End Get
    End Property
    Public ReadOnly Property State As String
        Get
            Return _State
        End Get
    End Property

    Public ReadOnly Property Name As String
        Get
            Return _Name
        End Get
    End Property

    Sub RefreshState()
        _State = If(IO.File.Exists(Me._filepath), "ready", "notfound")
        OnPropertyChanged("State")
    End Sub

    Sub OpenLocation(ByVal p As Object)
        Try
            Shell("explorer " & IO.Path.GetDirectoryName(_filepath), AppWinStyle.MaximizedFocus)
        Catch ex As Exception
            Windows.MessageBox.Show(ex.ToString())
        End Try
    End Sub

End Class

Public Class OutilProv
    Inherits VMBase


    Dim _Name As String

    Sub New(ByVal name As String)
        Me._Name = name
    End Sub


    Public ReadOnly Property State As String
        Get

        End Get
    End Property

    Public ReadOnly Property Name As String
        Get
            Return _Name
        End Get
    End Property

End Class


Public Class OutilProvCollection
    Inherits VMBase
    Implements INotifyCollectionChanged
    Implements IList(Of OutilProv)

    Private container As New ObservableCollection(Of OutilProv)

    Sub New()
        AddHandler container.CollectionChanged, AddressOf containerCollectionChanged
    End Sub

    Private Event CollectionChanged(ByVal sender As Object, ByVal e As System.Collections.Specialized.NotifyCollectionChangedEventArgs) Implements System.Collections.Specialized.INotifyCollectionChanged.CollectionChanged

    Public Sub Add(ByVal item As OutilProv) Implements System.Collections.Generic.ICollection(Of OutilProv).Add
        container.Add(item)
        OnPropertyChanged("Count")
    End Sub

    Public Sub Clear() Implements System.Collections.Generic.ICollection(Of OutilProv).Clear
        container.Clear()
        OnPropertyChanged("Count")
    End Sub

    Public Function Contains(ByVal item As OutilProv) As Boolean Implements System.Collections.Generic.ICollection(Of OutilProv).Contains
        Return container.Contains(item)
    End Function

    Private Sub CopyTo(ByVal array() As OutilProv, ByVal arrayIndex As Integer) Implements System.Collections.Generic.ICollection(Of OutilProv).CopyTo
        container.CopyTo(array, arrayIndex)
    End Sub

    Public ReadOnly Property Count As Integer Implements System.Collections.Generic.ICollection(Of OutilProv).Count
        Get
            Return container.Count
        End Get
    End Property

    Private ReadOnly Property IsReadOnly As Boolean Implements System.Collections.Generic.ICollection(Of OutilProv).IsReadOnly
        Get
            Return False
        End Get
    End Property

    Public Function Remove(ByVal item As OutilProv) As Boolean Implements System.Collections.Generic.ICollection(Of OutilProv).Remove
        Return container.Remove(item)
        OnPropertyChanged("Count")
    End Function

    Private Function GetEnumerator() As System.Collections.Generic.IEnumerator(Of OutilProv) Implements System.Collections.Generic.IEnumerable(Of OutilProv).GetEnumerator
        Return container.GetEnumerator()
    End Function

    Public Function IndexOf(ByVal item As OutilProv) As Integer Implements System.Collections.Generic.IList(Of OutilProv).IndexOf
        Return container.IndexOf(item)
    End Function

    Public Sub Insert(ByVal index As Integer, ByVal item As OutilProv) Implements System.Collections.Generic.IList(Of OutilProv).Insert
        container.Insert(index, item)
        OnPropertyChanged("Count")

    End Sub

    Default Public Property Item(ByVal index As Integer) As OutilProv Implements System.Collections.Generic.IList(Of OutilProv).Item
        Get
            Return container(index)
        End Get
        Set(ByVal value As OutilProv)
            container(index) = value
        End Set
    End Property

    Public Sub RemoveAt(ByVal index As Integer) Implements System.Collections.Generic.IList(Of OutilProv).RemoveAt
        container.RemoveAt(index)
        OnPropertyChanged("Count")
    End Sub

    Private Function GetEnumerator1() As System.Collections.IEnumerator Implements System.Collections.IEnumerable.GetEnumerator
        Return container.GetEnumerator()

    End Function

    Private Sub containerCollectionChanged(ByVal sender As Object, ByVal e As NotifyCollectionChangedEventArgs)
        RaiseEvent CollectionChanged(Me, e)
    End Sub

End Class

Public Class SCommand
    Implements ICommand

    Private _action As Action(Of Object)
    Sub New(ByVal action As Action(Of Object))
        Me._action = action
    End Sub

    Private Function CanExecute(ByVal parameter As Object) As Boolean Implements System.Windows.Input.ICommand.CanExecute
        Return True
    End Function

    Private Event CanExecuteChanged(ByVal sender As Object, ByVal e As System.EventArgs) Implements System.Windows.Input.ICommand.CanExecuteChanged

    Private Sub Execute(ByVal parameter As Object) Implements System.Windows.Input.ICommand.Execute
        _action(parameter)
    End Sub
End Class


